package com.lwj.liwanjia.view.fragment.supermarket;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.DetailPingjiaAdapter;
import com.lwj.liwanjia.adapter.MyGridAdapter;
import com.lwj.liwanjia.adapter.PingJiaAdapter;
import com.lwj.liwanjia.base.BaseFragment;
import com.lwj.liwanjia.bean.PingjiaDetailBean;
import com.lwj.liwanjia.bean.PingjiaLableBean;
import com.lwj.liwanjia.impl.PingjiaDetailView;
import com.lwj.liwanjia.impl.PingjiaLableView;
import com.lwj.liwanjia.presenter.PingjiaDetailPresenter;
import com.lwj.liwanjia.presenter.PingjiaLablePresenter;
import com.lwj.liwanjia.utils.MyGridView;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.login.LoginActivity;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * 商品评价
 */
public class PingjiaFragment extends BaseFragment implements PingjiaLableView, PingjiaDetailView {

    public static PingjiaFragment getInstance(String commodityCode, String commodityType) {
        PingjiaFragment fragment = new PingjiaFragment();
        Bundle bundle = new Bundle();
        bundle.putString("commodityCode", commodityCode);
        bundle.putString("commodityType", commodityType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @BindView(R.id.tv_haopingdu)
    TextView tvHaopingdu;
    @BindView(R.id.pingjia_grid)
    MyGridView pingjiaGrid;
    @BindView(R.id.pingjia_detail_rlv)
    SuperRecyclerView pingjiaDetail;

    private PingJiaAdapter adapter;
    private List<String> list = new ArrayList<>();//好评字段
    private DetailPingjiaAdapter detailPingjiaAdapter;
    private List<PingjiaDetailBean.DataBean> pingjiaList = new ArrayList<>();
    private PingjiaLablePresenter lablePresenter;
    private String token;
    private String commodityCode;
    private String commodityType;
    private PingjiaDetailPresenter detailPresenter;
    private Map<String, Object> map = new HashMap<>();
    private String str = "全部";

    @Override
    protected void initView(View view) {
        Bundle bundle = getArguments();
        if (bundle != null) {
            commodityCode = bundle.getString("commodityCode");
            commodityType = bundle.getString("commodityType");
        }
        adapter = new PingJiaAdapter(getActivity(), list);
        pingjiaGrid.setAdapter(adapter);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        pingjiaDetail.setLayoutManager(manager);
        pingjiaDetail.setLoadMoreEnabled(false);
        pingjiaDetail.setRefreshEnabled(false);
        detailPingjiaAdapter = new DetailPingjiaAdapter(getActivity(), pingjiaList);
        pingjiaDetail.setAdapter(detailPingjiaAdapter);
        token = SpfUtils.getSpfUtils(getActivity()).getToken();
        lablePresenter = new PingjiaLablePresenter(this);
        detailPresenter = new PingjiaDetailPresenter(this);
        listenter();
    }

    private void listenter() {
        pingjiaGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                adapter.setCheckPosition(position);
                adapter.notifyDataSetChanged();
                detailPresenter.getPingjiaDetail(token, list.get(position).substring(0, 2), commodityCode, commodityType);
            }
        });
    }

    @Override
    protected void initData() {
        lablePresenter.getPingjiaLable(token, commodityCode, commodityType);
        detailPresenter.getPingjiaDetail(token, str, commodityCode, commodityType);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_pingjia;
    }


    @Override
    public void showError(Exception e) {
        Toast.makeText(getActivity(), "数据加载失败！", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getPingjiaDetal(PingjiaDetailBean pingjiaDetailBean) {
        if (pingjiaDetailBean.getState() == 0) {
        } else if (pingjiaDetailBean.getState() == 1) {
            pingjiaList.clear();
            if (pingjiaDetailBean.getData() != null && pingjiaDetailBean.getData().size() > 0) {
                pingjiaList.addAll(pingjiaDetailBean.getData());
            }
            detailPingjiaAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getPingjiaLable(PingjiaLableBean pingjiaLableBean) {
        if (pingjiaLableBean.getState() == 0) {
            startActivity(new Intent(getActivity(), LoginActivity.class));
            getActivity().finish();
        } else {
            PingjiaLableBean.DataBean dataBean = pingjiaLableBean.getData().get(0);
            tvHaopingdu.setText(dataBean.getLimit());
            list.clear();
            list.addAll(dataBean.getLabel());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (detailPresenter != null) {
            detailPresenter = null;
        }
        if (lablePresenter != null) {
            lablePresenter = null;
        }
    }
}
